package Beans;

import BO.Service;
import UI.UserUI;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

public class LoginBean implements Serializable{

    private String name;
    //private Boolean loggedIn = false;
    private String password;

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }

    public Boolean getLoggedIn() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (session.getAttribute("User") != null) {

            return true;

        } else {

            return false;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String validate() {
        UI.UserUI user = Service.validateUser(name, password);

        if (user != null) {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            session.setAttribute("User", user);
            //loggedIn = true;
            return "true";
        }
        return "false";
    }

    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("User", null);
        name = "";
        password = "";
        return "true";
    }
}
